﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using DevDefined.OAuth.Framework;
using Lightray.Hattrick.ChppLib;

namespace Lightray.Hattrick
{
    public class Chpp
    {     
        private const string CONSUMER_KEY = "JSbyUK14ITfQDTSQOsHFU-";

        private Action<FetchResultEventArgs> _downloadFinishedCallback;

        public void DownloadPlayersAndYouthPlayersDetails(Action<FetchResultEventArgs> callback)
        {
            _downloadFinishedCallback = callback;

            WebFetcher fetcher = new WebFetcher(readTokenFromFile(), readSecretFromFile());            
            fetcher.FetchComplete += new EventHandler<FetchResultEventArgs>(fetcher_FetchComplete);
            fetcher.Fetch(FetchRequest.MyPlayers | FetchRequest.MyYouthPlayers);            
        }

        private  void fetcher_FetchComplete(object sender, FetchResultEventArgs e)
        {
            _downloadFinishedCallback(e);
        }

        private  string readSecretFromFile()
        {
            return File.ReadAllText("secret.txt");
        }

        private  IToken readTokenFromFile()
        {
            BinaryFormatter bf = new BinaryFormatter();
            return bf.Deserialize(File.OpenRead("mytoken.bin")) as IToken;
        }
    }
}
